$(function(){
	var provinceSelect = $($("#province"));
	var citySelect = $($("#city"));
	var countrySelect = $($("#country"));
	var townSelect = $($("#town"));
	var villageSelect = $($("#village"));
	//加载联动树
	//1.加载省
	$.ajax({
		url: ctx + "/area/province",
		dataType: "json",
		success: function(json){
			if(json.code == 0){
				var provinces = json.provinces;
				if(provinces && provinces.length > 0){
					var province;//省
					var city;//市
					var country;//区县
					var town;//乡镇
					var village;//街道
					
					//1.加载省
					provinceSelect.find("option").remove();
					for(var i = 0; i < provinces.length; i++){
						province = provinces[i];
						if (province.id=='604794'){
							provinceSelect.append("<option id='province' selected value='" + province.id + "'>" + province.name + "</option>");
						}else {
							provinceSelect.append("<option id='province' value='" + province.id + "'>" + province.name + "</option>");
						}
					}
					
					//2.加载选中省的市
					province = document.getElementById("province").value;
					$.ajax({
						url: ctx + "/area/query/" + province,
						dataType: "json",
						success: function(json){
							if(json.code == 0){
								var cities = json.areas;
								if(!cities || cities.length == 0){
									return;
								}
								var city;
								for(var i = 0; i < cities.length; i++){
									city = cities[i];
									if (city.id=='619765') {
										citySelect.append("<option id='city' selected value='" + city.id + "'>" + city.name + "</option>");
									}else{
										citySelect.append("<option id='city'  value='" + city.id + "'>" + city.name + "</option>");
									}
								}
								//3.加载选中市的区县
								city = document.getElementById("city").value;
								$.ajax({
									url: ctx + "/area/query/" + city,
									dataType: "json",
									success: function(json){
										if(json.code == 0){
											var countries = json.areas;
											if(!countries || countries.length == 0){
												return;
											}
											var country;
											for(var i = 0; i < countries.length; i++){
												country = countries[i];
												countrySelect.append("<option value='"+country.id+"'>"+country.name+"</option>");
											}
											//4.加载第一个区县的乡镇
											country = countries[0];
											$.ajax({
												url: ctx + "/area/query/" + country.id,
												dataType: "json",
												success: function(json){
													if(json.code == 0){
														var towns = json.areas;
														if(!towns || towns.length == 0){
															return;
														}
														var town;
														for(var i = 0; i < towns.length; i++){
															town = towns[i];
															townSelect.append("<option value='"+town.id+"'>"+town.name+"</option>");
														}
														//5.加载第一个乡镇的街道
														town = towns[0];
														$.ajax({
															url: ctx + "/area/query/" + town.id,
															dataType: "json",
															success: function(json){
																if(json.code == 0){
																	var villages = json.areas;
																	if(!villages || villages.length == 0){
																		return;
																	}
																	var village;
																	for(var i = 0; i < villages.length; i++){
																		village = villages[i];
																		villageSelect.append("<option value='"+village.id+"'>"+village.name+"</option>");
																	}
																}                   
															}
													   });
													}                   
												}
										   });
										}                   
									}
							   });
							}                   
						}
				   }); 
				}
			}else{
				layer.msg(json.msg, {icon: 5});
			}                    
		}
	});
	$("#province").change(function(){
		$("#city,#country,#town,#village").find("option").remove();
		var option = $("#province option:selected");
		$.ajax({
			url: ctx + "/area/query/" + option.val(),
			dataType: "json",
			success: function(json){
				if(json.code == 0){
					var cities = json.areas;
					if(!cities || cities.length == 0){
						return;
					}
					var city;
					for(var i = 0; i < cities.length; i++){
						city = cities[i];
						citySelect.append("<option value='"+city.id+"'>"+city.name+"</option>");
					}
					//3.加载第一个市的区县
					city = cities[0];
					$.ajax({
						url: ctx + "/area/query/" + city.id,
						dataType: "json",
						success: function(json){
							if(json.code == 0){
								var countries = json.areas;
								if(!countries || countries.length == 0){
									return;
								}
								var country;
								for(var i = 0; i < countries.length; i++){
									country = countries[i];
									countrySelect.append("<option value='"+country.id+"'>"+country.name+"</option>");
								}
								//4.加载第一个区县的乡镇
								country = countries[0];
								$.ajax({
									url: ctx + "/area/query/" + country.id,
									dataType: "json",
									success: function(json){
										if(json.code == 0){
											var towns = json.areas;
											if(!towns || towns.length == 0){
												return;
											}
											var town;
											for(var i = 0; i < towns.length; i++){
												town = towns[i];
												townSelect.append("<option value='"+town.id+"'>"+town.name+"</option>");
											}
											//5.加载第一个乡镇的街道
											town = towns[0];
											$.ajax({
												url: ctx + "/area/query/" + town.id,
												dataType: "json",
												success: function(json){
													if(json.code == 0){
														var villages = json.areas;
														if(!villages || villages.length == 0){
															return;
														}
														var village;
														for(var i = 0; i < villages.length; i++){
															village = villages[i];
															villageSelect.append("<option value='"+village.id+"'>"+village.name+"</option>");
														}
													}                   
												}
										   });
										}                   
									}
							   });
							}                   
						}
				   });
				}                   
			}
	   }); 
	});
	$("#city").change(function(){
		$("#country,#town,#village").find("option").remove();
		//3.加载第一个市的区县
		var option = $("#city option:selected");
		$.ajax({
			url: ctx + "/area/query/" + option.val(),
			dataType: "json",
			success: function(json){
				if(json.code == 0){
					var countries = json.areas;
					if(!countries || countries.length == 0){
						return;
					}
					var country;
					for(var i = 0; i < countries.length; i++){
						country = countries[i];
						countrySelect.append("<option value='"+country.id+"'>"+country.name+"</option>");
					}
					//4.加载第一个区县的乡镇
					country = countries[0];
					$.ajax({
						url: ctx + "/area/query/" + country.id,
						dataType: "json",
						success: function(json){
							if(json.code == 0){
								var towns = json.areas;
								if(!towns || towns.length == 0){
									return;
								}
								var town;
								for(var i = 0; i < towns.length; i++){
									town = towns[i];
									townSelect.append("<option value='"+town.id+"'>"+town.name+"</option>");
								}
								//5.加载第一个乡镇的街道
								town = towns[0];
								$.ajax({
									url: ctx + "/area/query/" + town.id,
									dataType: "json",
									success: function(json){
										if(json.code == 0){
											var villages = json.areas;
											if(!villages || villages.length == 0){
												return;
											}
											var village;
											for(var i = 0; i < villages.length; i++){
												village = villages[i];
												villageSelect.append("<option value='"+village.id+"'>"+village.name+"</option>");
											}
										}                   
									}
							   });
							}                   
						}
				   });
				}                   
			}
	   });
	});
	$("#country").change(function(){
		$("#town,#village").find("option").remove();
		//4.加载第一个区县的乡镇
		var option = $("#country option:selected");
		$.ajax({
			url: ctx + "/area/query/" + option.val(),
			dataType: "json",
			success: function(json){
				if(json.code == 0){
					var towns = json.areas;
					if(!towns || towns.length == 0){
						return;
					}
					var town;
					for(var i = 0; i < towns.length; i++){
						town = towns[i];
						townSelect.append("<option value='"+town.id+"'>"+town.name+"</option>");
					}
					//5.加载第一个乡镇的街道
					town = towns[0];
					$.ajax({
						url: ctx + "/area/query/" + town.id,
						dataType: "json",
						success: function(json){
							if(json.code == 0){
								var villages = json.areas;
								if(!villages || villages.length == 0){
									return;
								}
								var village;
								for(var i = 0; i < villages.length; i++){
									village = villages[i];
									villageSelect.append("<option value='"+village.id+"'>"+village.name+"</option>");
								}
							}                   
						}
				   });
				}                   
			}
	   });
	});
	$("#town").change(function(){
		$("#village").find("option").remove();
		//5.加载第一个乡镇的街道
		var option = $("#town option:selected");
		$.ajax({
			url: ctx + "/area/query/" + option.val(),
			dataType: "json",
			success: function(json){
				if(json.code == 0){
					var villages = json.areas;
					if(!villages || villages.length == 0){
						return;
					}
					var village;
					for(var i = 0; i < villages.length; i++){
						village = villages[i];
						villageSelect.append("<option value='"+village.id+"'>"+village.name+"</option>");
					}
				}                   
			}
	   });
	});
});